/**
 * DOM 操作封装
 */
// 引入
const local=require('./local.js');

/* 通过id获取dom */
function getEle(id) {
    return document.querySelector(id)
}

/* 通过类名获取dom */
function getEleAll(className) {
    return document.querySelectorAll(className)
}

/* dom加载完毕 */
function ready(callback) {
    /* 
    文档加载事件:
        onload: 当页面所有资源(html,css,img,video,...)加载完成后,才执行js代码
        onDOMContentLoaded: 只要html结构加载完成,就可以执行js代码
    
    */
    window.addEventListener('DOMContentLoaded', function () {
        callback()//调用方法  调用传入的方法
    })
}


// 封装底部菜单
function bMenu(m){
    getEle('footer').innerHTML=`
    <ul class="df jcb">
    <li class="${m=='home'?'footer-active':''}">
        <a href="home.html" class="dib tcenter">
            <span class="iconfont icon-shouye"></span>
            <p class="f16 ">首页</p>
        </a>
    </li>
    <li class="${m=='run'?'footer-active':''}">
        <a href="run.html" class="dib tcenter">
            <span class="iconfont icon-xintiaojiankangyundongyiliaobaohuxinshuai
            "></span>
            <p class="f16 ">运动</p>
        </a>
    </li>
    <li class="${m=='circle'?'footer-active':''}">
        <a href="circle.html" class="dib tcenter">
            <span class="iconfont icon-quanzi
            "></span>
            <p class="f16 ">圈子</p>
        </a>
    </li>
    <li class="${m=='information'?'footer-active':''}">
        <a href="information.html" class="dib tcenter">
            <span class="iconfont icon-wode
            "></span>
            <p class="f16 ">我的</p>
        </a>
    </li>
</ul>`
}
// 封装头部菜单
function hMenu(n){
    getEle('.runHeader').innerHTML=`
    <ul class="df jca">
    <li>
        <a class="${n=='run'?'run-active':''}" href="run.html">         
            <p>跑步</p>
        </a>
    </li>
    <li>
        <a class="${n=='riding'?'run-active':''}" href="riding.html">
            <p>骑行</p>
        </a>
    </li>
    <li>
        <a class="${n=='course'?'run-active':''}" href="course.html">
            <p>课程训练</p>
        </a>
    </li>
</ul>
    `
}

// 判定是否登录成功，如果没有登录，直接跳转到login,如果有登陆，想去哪里去哪里
function isLogin(){
    // 1.获取token
    let token=localStorage.getItem('tk')||'';
    // console.log(token);
    // 判定
    if(token.length==0){
        window.location.href='login.html'
    }
    console.log(token.length);
}

// 回退封装
function goback(){
   getEle('#backBtn').addEventListener('click',function(){
        window.history.go(-1);
    })
}



// 暴露出去
module.exports = {
    getEle,
    getEleAll,
    ready,
    bMenu,
    isLogin,
    hMenu,
    goback
    
}
